<template>
  <div class="about">
    <el-form
      :model="ruleForm"
      status-icon
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="pass">
        <el-input
          type="text"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="age">
        <el-input v-model.number="ruleForm.age"></el-input>
      </el-form-item>
      <!-- 头像上传 -->
      <div class="upload">
        <el-upload
          class="avatar-uploader"
          action="/api/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <!-- 图片上传 -->

      <span v-html="imgurl"></span>
      <el-form-item>
        <el-button type="primary" :plain="true" @click="submitForm('ruleForm')"
          >登录</el-button
        >
        <el-button @click="resetForm('ruleForm')">注册</el-button>
        <el-button @click="gieuesr">第三方登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { apiLogin, apiGetYzm } from "../api/index";
import {getUser, getcode} from "../ulits/threeUp"
export default {
  data() {
    return {
      imageUrl: "",
      ruleForm: {
        pass: "",
        checkPass: "",
        age: "",
      },
      imgurl: "",
    };
  },
  mounted() {
    apiGetYzm().then((res) => {
      this.imgurl = res.data;
    });
    getUser((data,token)=>{
      console.log(data.avatar_url);
      console.log(token);
    })
  },
  methods: {
    //第三方登录
    gieuesr(){
      getcode()
    },

    // 图片上传
    handleAvatarSuccess(res, file, fileList) {
      console.log(file.response);
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(this.imageUrl);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 图片上传

    async submitForm() {
      let name = this.ruleForm.pass;
      let pwd = this.ruleForm.checkPass;
      let yzm = this.ruleForm.age;
      const res = await apiLogin({ name, pwd, yzm });
      if (res.data.code === 200) {
        window.location.href = "http://localhost:8080";
        this.$message({
          message: res.data.msg,
          type: "success",
        });
      } else {
        this.$message.error(res.data.msg);
      }
    },
    resetForm() {},
  },
};
</script>

<style lang="scss" scoped>
.about {
  width: 500px;
  margin: 100px auto;
}
.upload {
  width: 190px;
  margin: 0 auto;
}
.avatar-uploader,
.el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader,
.el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 190px;
  height: 190px;
  line-height: 190px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
